import { InferAttributes } from 'sequelize'
import { Column, DataType, Model, PrimaryKey, Table } from 'sequelize-typescript'

/**
 * 奖池表实例
 */
@Table({ tableName: 'yx_system_config', underscored: true, timestamps: false })
export class SystemConfig extends Model<InferAttributes<SystemConfig>> {
    @PrimaryKey
    @Column(DataType.BIGINT)
    declare id: number

    @Column
    declare menu_name: string

    @Column
    declare value: string

    static getValue(key: string): Promise<string>
    static getValue(key1: string, key2: string, ...keys: string[]): Promise<Record<string, string>>
    static async getValue(...keys: string[]): Promise<any> {
        if (keys.length === 0) {
            return {}
        }
        if (keys.length === 1) {
            const row = await SystemConfig.findOne({ where: { menu_name: keys[0] } })
            return row ? row.value : ''
        }
        const rows = await SystemConfig.findAll({ where: { menu_name: keys } })
        return Object.fromEntries(rows.map((row) => [row.menu_name, row.value]))
    }
}
